load(
    "//tools/skylark:drake_cc.bzl",
    "drake_cc_googletest",
)
load("//tools/lint:lint.bzl", "add_lint_tests")
load("//tools/skylark:drake_data.bzl", "models_filegroup")
load("//tools/install:install_data.bzl", "install_data")

package(default_visibility = ["//visibility:public"])

# Convert the *.stl meshes to *.obj meshes, because many geometry and
# visualization tools in Drake only support *.obj meshes.

_STLS = glob(
    ["models/**/*.stl"],
    allow_empty = False,
)

_OBJS = [x[:-3] + "obj" for x in _STLS]

[
    # Recall this about genrules:
    #
    # - `srcs` are built for the "target" configuration, which is what all of
    #   our other programs and tests use; these will have a nice warm cache.
    #
    # - `tools` are built for the "exec" configuration, which is only ever used
    #   for genrules (or `tools` for any other flavor of rule); any large
    #   programs here will be costly to rebuild completely from scratch just
    #   for the genrule.
    #
    # Therefore, we add `stl2obj` as `srcs` (not `tools`) to avoid building it
    # in the "exec" configuration all by its lonesome self. We want to use the
    # same build of its dependencies as all of our regular code, for a smaller
    # footprint and better caching.
    genrule(
        name = obj + "_genrule",
        srcs = [stl] + ["//manipulation/util:stl2obj"],
        outs = [obj],
        cmd = " ".join([
            "env TSAN_OPTIONS=report_bugs=0",
            "$(location //manipulation/util:stl2obj)",
            "--input=$(location {})".format(stl),
            "--output=$@",
        ]),
        visibility = ["//visibility:private"],
    )
    for stl, obj in zip(_STLS, _OBJS)
]

models_filegroup(
    name = "models",
    extra_srcs = _OBJS + [
        "README.md",
    ],
)

install_data(
    name = "install_data",
    data = [":models"],
)

# === test/ ===

drake_cc_googletest(
    name = "load_pr2_simplified_test",
    data = [":models"],
    deps = [
        "//multibody/parsing",
    ],
)

add_lint_tests()
